package org.jeecg.modules.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.dingtalk.api.response.OapiMessageCorpconversationGetsendresultResponse;
import com.taobao.api.ApiException;
import org.apache.poi.ss.formula.functions.T;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DySmsEnum;
import org.jeecg.common.util.DySmsHelper;
import org.jeecg.modules.service.IDiyProfessorInfoService;
import org.jeecg.modules.util.SelectSendMessage;
import org.jeecg.modules.util.SendMessageToDingding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 王鹏
 * @version 1.0
 * @description   实现短信，钉钉发送消息
 * @date 2020/8/14 20:09
 */
@RestController
@RequestMapping("/sendmessage")
public class SendMessage {

    @Autowired
    private IDiyProfessorInfoService diyProfessorInfoService;

    //钉钉和短信同时发送消息
    @RequestMapping("/all")
    public Result<?> sendedaunxin(@RequestBody JSONObject jsonObject) throws ClientException, ApiException {
        Result<T> result = new Result();

        JSONArray prefessorid = jsonObject.getJSONArray("prefessorid");
        String msg = jsonObject.getString("modalText");
        for(int a=0;a<prefessorid.size();a++){
            String id = (String) prefessorid.get(a);
            String phone = diyProfessorInfoService.selPhone(id);
            String dinguserid = diyProfessorInfoService.getDingDinguserid(id);
            SendMessageToDingding.testsendmessage(dinguserid,msg);
            JSONObject obj1 = new JSONObject();
            obj1.put("name", msg);
            DySmsHelper.sendSms(phone,obj1, DySmsEnum.DING_TMPLATE_CODE);
        }
        result.setSuccess(true);
        return result;
    }
    //短信发送消息
    @RequestMapping("/duanxin")
    public Result<?> sendeall(@RequestBody JSONObject jsonObject) throws ClientException {
        Result<T> result = new Result();

        JSONArray prefessorid = jsonObject.getJSONArray("prefessorid");
        String msg = jsonObject.getString("modalText");
        for(int a=0;a<prefessorid.size();a++){
            String id = (String) prefessorid.get(a);
            String phone = diyProfessorInfoService.selPhone(id);
            JSONObject obj1 = new JSONObject();
            obj1.put("name", msg);
            DySmsHelper.sendSms(phone,obj1, DySmsEnum.DING_TMPLATE_CODE);
        }
        result.setSuccess(true);
        return result;
    }
    //钉钉发送消息
    @RequestMapping("/dingding")
    public Result<?> senddingding(@RequestBody JSONObject jsonObject) throws ClientException, ApiException {
        Result<T> result = new Result();

        JSONArray prefessorid = jsonObject.getJSONArray("prefessorid");
        String msg = jsonObject.getString("modalText");
        for(int a=0;a<prefessorid.size();a++){
            String id = (String) prefessorid.get(a);
            String dinguserid = diyProfessorInfoService.getDingDinguserid(id);
            //发送消息，并且返回taskid
            SimpleDateFormat formatter= new SimpleDateFormat("HH:mm:ss");
            Date date = new Date(System.currentTimeMillis());
            Long taskId = SendMessageToDingding.testsendmessage(dinguserid, "通知时间："+formatter.format(date)+"\n"+"通知内容："+msg);
            OapiMessageCorpconversationGetsendresultResponse response = SelectSendMessage.selectsendmessage(taskId);
            System.out.println(response.getBody());
        }
        result.setSuccess(true);
        return result;
    }
}
